package cn.virens.web.controller.admin.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.virens.database.mysql.model.sys.SysMessage;
import cn.virens.database.mysql.model.sys.SysUser;
import cn.virens.database.service.sys.SysMessageService;
import cn.virens.database.service.sys.SysResourceService;
import cn.virens.web.common.BaseUserController;

@Controller
@RequestMapping("/admin/adminlte")
public class AdminLTEController extends BaseUserController {
	private @Resource SysMessageService sysMessageService;
	private @Resource SysResourceService sysResourceService;

	/**
	 * 获取用户信息
	 * 
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "user.json")
	public Model user(HttpServletRequest request, Model model) {
		SysUser user = getUser(request);

		if (user != null) {
			return renderSuccess(user, model);
		} else {
			return renderFail(model, "用户信息为空");
		}
	}

	/**
	 * 获取菜单
	 * 
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "menu.json")
	public Model getMenusByAccount(HttpServletRequest request, Model model) {
		List<?> menus = getAttribute(request, "_menu", List.class);
		if (menus == null) {
			menus = sysResourceService.getMenusByAccount(getAccount(request));
		}

		return renderSuccess(menus, model);
	}

	/**
	 * 获取最新通知消息
	 * 
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "notifications.json")
	public Model notifications(HttpServletRequest request, Model model) {
		PageInfo<SysMessage> result = sysMessageService.notifications(getAccount(request));

		model.addAttribute("total", result.getTotal());
		model.addAttribute("list", result.getList());
		model.addAttribute("success", true);

		return model;
	}
}
